#include "snakegame.h"
#include<QPainter>
#include<qpainter.h>
#include<QTime>
#include<QMessageBox>
#include<QWidget>

SnakeGame::SnakeGame(QWidget*parent):QWidget(parent){
    setStyleSheet("background-color:black;");
    setFixedSize(600,600);
    setWindowTitle("Qt贪吃蛇");

    timer=new QTimer(this);
    connect(timer,&QTimer::timeout,this,&SnakeGame::updateGame);

    initGame();
}

void SnakeGame::initGame(){
    snake.clear();
    snake.append(QPoint(100,100));
    snake.append(QPoint(80,100));
    snake.append(QPoint(60,100));

    direction=RIGHT;
    inGame=true;
    score=0;

    locateFood();
    timer->start(150);
}

void SnakeGame::locateFood(){
    QTime time =QTime::currentTime();
    srand((uint)time.msec());

    int x=(rand()%RAND_POS)*DOT_SIZE;
    int y=(rand()%RAND_POS)*DOT_SIZE;

    food=QPoint(x,y);
}

void SnakeGame::updateGame(){
    if(inGame){
        checkFood();
        move();
        checkCollision();
    }
    repaint();
}

void SnakeGame::paintEvent(QPaintEvent*event){
    Q_UNUSED(event);
    QPainter qp(this);

    if(inGame){
        draw(qp);
    }else{
        gameOver(qp);
    }
}

void SnakeGame::draw(QPainter &qp){

    qp.setBrush(Qt::red);
    qp.drawEllipse(food.x(),food.y(),DOT_SIZE,DOT_SIZE);

    qp.setBrush(Qt::green);
    for(const QPoint &segment:snake){
        qp.drawRect(segment.x(),segment.y(),DOT_SIZE,DOT_SIZE);
    }

    qp.setPen(Qt::white);
    qp.setFont(QFont("Arial",14));
    qp.drawText(10,20,QString("分数：%1").arg(score));
}

void SnakeGame::gameOver(QPainter &qp){
    QString message="游戏结束！\n";
    message +=QString("最终得分：%1\n").arg(score);
    message+="按空格重新再来";

    qp.setPen(Qt::white);
    qp.setFont(QFont("Arial",16));
    qp.drawText(rect(),Qt::AlignCenter,message);
}

void SnakeGame::keyPressEvent(QKeyEvent*event){
    int key=event->key();

    if(key==Qt::Key_Space&&!inGame){
        initGame();
        return;
    }

    if(!inGame) return;

    switch(key){
    case Qt::Key_Left:
        if(direction!=RIGHT) direction=LEFT;
        break;
    case Qt::Key_Right:
        if(direction!=LEFT)direction=RIGHT;
        break;
    case Qt::Key_Up:
        if(direction!=DOWN)direction=UP;
        break;
    case Qt::Key_Down:
        if(direction!=UP)direction=DOWN;
        break;
    }
}

void SnakeGame::checkCollision(){
    if(snake[0].x()>=width()||snake[0].x()<0||snake[0].y()>=height()||snake[0].y()<0){
        inGame=false;
    }

    for(int i=1;i<snake.size();++i){
        if(snake[0]==snake[i]){
            inGame=false;
            break;
        }
    }

    if(!inGame){
        timer->stop();
    }
}

void SnakeGame::move(){
    for(int i=snake.size()-1;i>0;--i){
        snake[i]=snake[i-1];
    }

    switch(direction){
    case UP:
        snake[0].ry()-=DOT_SIZE;
        break;
    case DOWN:
        snake[0].ry()+=DOT_SIZE;
        break;
    case LEFT:
        snake[0].rx()-=DOT_SIZE;
        break;
    case RIGHT:
        snake[0].rx()+=DOT_SIZE;
        break;
    }
}

void SnakeGame::checkFood(){
    if(snake[0]==food){
        snake.append(QPoint(-1,-1));
        locateFood();
        score+=10;

        if(score%50==0&&timer->interval()>50){
            timer->setInterval(timer->interval()-10);
        }
    }
}

/*Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}
*/
